package com.test.mytest.saolei.my.swt;


import java.util.Timer;
import java.util.TimerTask;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

/*
 * Created on 2004-8-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author TdbT
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Miner {
    public static Display mydisplay;
    static int countnum = 10;
    static int rightnum = 0;
    static boolean start = false;
    static int time;
    static Timer timer;
    public static void main(String[] args) {
        Miner.mydisplay = new Display();
        Miner miner = new Miner();
        
        miner.runSpace(mydisplay);
    }

    /**
     * @param display
     */
    public void runSpace(final Display display) {
        //new a shell to hold the items
        final Shell shell = new Shell(display);
        //setSize150*235
        shell.setSize(155, 240);
        //the title is "Miner"
        shell.setText("Miner");
        //this button is to start the game
        Shell child = new Shell(shell, SWT.BORDER);
        Button start = new Button(shell, SWT.PUSH);
        //setSize
        start.setSize(30, 30);
        //setLocation
        start.setLocation(56, 6);
//        ImageData imdt = new ImageData(getClass()
//                .getResourceAsStream("face1.gif"));
//        Image icon = new Image(display, imdt);
//        start.setImage(icon);
        Composite mineplace = new Composite(shell, SWT.BORDER);
        Label label1 = new Label(shell, SWT.BORDER);
        label1.setSize(42, 30);
        label1.setLocation(6, 6);
        label1.setFont(new Font(display, "Courier", 20, SWT.NORMAL));
        Label label2 = new Label(shell, SWT.BORDER);
        label2.setSize(48, 30);
        label2.setLocation(95, 6);
        label2.setFont(new Font(display, "Courier", 20, SWT.NORMAL));
        timer=new Timer();
        timer.schedule(new TimeShowTask(), 0,1 * 1000);
        
        //System.out.println(Miner.start);
        Mine mines = new Mine(mineplace, label1, start);
        mines.initiate(display);
        start.addSelectionListener(new StartSelection(mines, shell, 
display,start));
        //MenuBAR
        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        //MenuItem
        MenuItem gameitem = new MenuItem(menu, SWT.CASCADE);
        gameitem.setText("Game");
        //dropdown menu
        Menu gamemenu = new Menu(shell, SWT.DROP_DOWN);
        gameitem.setMenu(gamemenu);
        //menuitem in the dropdown menu
        MenuItem newitem = new MenuItem(gamemenu, SWT.PUSH);
        newitem.setText("New");
        newitem.addSelectionListener(new 
StartSelection(mines,shell,display,start));
        MenuItem exititem = new MenuItem(gamemenu, SWT.PUSH);
        exititem.setText("exit");
        exititem.addListener(SWT.Selection,new Listener(){
            public void handleEvent(Event e){
                System.exit(1);
            }
        });
        //open the shell
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()){
                display.sleep();
                label2.setText(Integer.toString(Miner.time));
            }
        }
        
    }

    
    
}
class TimeShowTask extends TimerTask {

    public void run() {
        if (Miner.start == true) {

            Miner.time++;
        } 
        if(Miner.time>0&&Miner.start==false)
            Miner.timer.cancel();
    }

}
class StartSelection extends SelectionAdapter {
    Mine mines;

    Shell shell;

    Display display;
    Button start;
    public StartSelection(Mine mines, Shell shell, Display display,Button 
start) {
        this.mines = mines;
        this.display = display;
        this.shell = shell;
        this.start=start;
    }

    public void widgetSelected(SelectionEvent e) {
        ImageData imdt = new ImageData(getClass()
                .getResourceAsStream("face1.gif"));
        Image icon = new Image(display, imdt);
        start.setImage(icon);
        mines.mineplace.setEnabled(true);
        mines.refreshData();
        mines.refreshButton();
        Miner.timer=new Timer();
        Miner.timer.schedule(new TimeShowTask(), 0,1 * 1000);
    }
} 